//
//  HomePageSegmentedControl.m
//  ShowYork
//
//  Created by hxh on 2021/3/9.
//  Copyright © 2021 hxh. All rights reserved.
//

typedef void(^doneBlock)(NSInteger index);
typedef void(^backBlock)(void);
#import "HomePageSegmentedControl.h"

@interface HomePageSegmentedControl ()
@property (nonatomic,strong)UIButton *recommendBtn;
@property (nonatomic,strong)UIButton *momentBtn;
@property (nonatomic,strong)UILabel *recommentLb;
@property (nonatomic,strong)UILabel *momentLb;
@property (nonatomic,strong)UIImageView *lineImgView;
@property (nonatomic,copy)doneBlock done;
@property (nonatomic,copy)backBlock back;
@property (nonatomic,strong)UIButton *backBtn;
@property (nonatomic,strong)UIImageView *backImgView;
@end

@implementation HomePageSegmentedControl

- (instancetype)initWithCompleteBlock:(void (^)(NSInteger))completeBlock{
    if (self = [super init]) {
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
        [self setUI];
    }
    return self;
}
- (instancetype)initWithCompleteBlock:(void (^)(NSInteger))completeBlock backBlock:(void (^)(void))backBlock{
    if (self = [super init]) {
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
        if (backBlock) {
            self.back = ^{
                backBlock();
            };
        }
        [self setUI];
    }
    return self;
}
- (void)setUI{
    self.frame = CGRectMake(0, GK_STATUSBAR_HEIGHT, kScreenW, 44);
        
    self.lineImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_segment_line_tag"]];
    [self addSubview:self.lineImgView];

    
    self.recommendBtn = [[UIButton alloc]init];
    [self.recommendBtn addTarget:self action:@selector(recommendMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.recommendBtn];
    
    self.momentBtn = [[UIButton alloc]init];
    [self.momentBtn addTarget:self action:@selector(momentMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.momentBtn];
    
    self.recommentLb = [[UILabel alloc]init];
    self.recommentLb.text = @"广场";
    self.recommentLb.textColor = RGBA(35, 24, 21, 1);
    self.recommentLb.font = [UIFont boldSystemFontOfSize:18];
    [self addSubview:self.recommentLb];
    
    self.momentLb = [[UILabel alloc]init];
    self.momentLb.text = @"关注";
    self.momentLb.textColor = RGBA(35, 24, 21, 1);
    self.momentLb.font = [UIFont boldSystemFontOfSize:18];
    [self addSubview:self.momentLb];
    
    
    [self.recommentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(-50);
        make.top.bottom.equalTo(self);
    }];
    
    [self.momentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(50);
        make.top.bottom.equalTo(self);
    }];
    
    [self.recommendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.left.right.equalTo(self.recommentLb);
    }];
    [self.momentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.left.right.equalTo(self.momentLb);
    }];
    
    [self.lineImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.recommentLb);
        make.bottom.equalTo(self.recommentLb.mas_bottom).offset(-14);
    }];
    
    self.backImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_back_tag"]];
    [self addSubview:self.backImgView];
    [self.backImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(16);
        make.centerY.equalTo(self.recommentLb);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    self.backBtn = [[UIButton alloc]init];
    [self.backBtn addTarget:self action:@selector(backMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.backBtn];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
}

- (void)backMethod{
    self.back();
}

- (void)recommendMethod{
    [UIView animateWithDuration:0.25 animations:^{
        [self.lineImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.recommentLb);
            make.bottom.equalTo(self.recommentLb.mas_bottom).offset(-14);
        }];
        [self layoutIfNeeded];
    }];
    self.done(0);
    
}
- (void)momentMethod{
    [UIView animateWithDuration:0.25 animations:^{
        [self.lineImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.momentLb);
            make.bottom.equalTo(self.momentLb.mas_bottom).offset(-14);
        }];
        [self layoutIfNeeded];
    }];
    
    self.done(1);
}

@end
